<template>
  <div class="login-div">
    <div class="login-c">
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="login-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="用户名" prop="userName">
          <el-input :suffix-icon="Calendar" v-model="ruleForm.userName" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input :suffix-icon="Calendar" v-model="ruleForm.pwd" type="password"
            autocomplete="off"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
          <a @click="resetForm(ruleFormRef)">忘记密码？</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {  ref, reactive} from 'vue';
import { useRouter } from 'vue-router'

const ruleFormRef = ref(null)
//先在setup中定义
const router = useRouter()
const formSize = ref('default')
const ruleForm = reactive({
  userName: "",
  pwd: ""
})
const rules = reactive({
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 5, message: '名字长度 3 至 5', trigger: 'blur' },
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 10, message: '名字长度 6 至 10', trigger: 'blur' },
  ],
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      router.push({ path: '/home' })
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
<script setup></script>

<style lang="scss" scoped>
.login-div {
  width: 100vw;
  height: 100vh;
}
.login-c {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
}
</style>
